package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.AddCourseTeacherDto;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @Override
    public CourseTeacher selectTeacher(Long id) {
        LambdaQueryWrapper<CourseTeacher> qe = new LambdaQueryWrapper<>();
        qe.eq(CourseTeacher::getCourseId,id);
        return courseTeacherMapper.selectOne(qe);
    }

    @Override
    public CourseTeacher addTeacher(AddCourseTeacherDto addCourseTeacherDto) {
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(addCourseTeacherDto,courseTeacher);
        int insert = courseTeacherMapper.insert(courseTeacher);
        if(insert <= 0){
            XueChengPlusException.cast("新增教师失败");
        }
        return courseTeacher;
    }


    @Override
    public CourseTeacher editTeacher(CourseTeacher courseTeacher) {
        CourseTeacher courseTeacher1 = courseTeacherMapper.selectById(courseTeacher.getId());
        if(courseTeacher1 == null){
            XueChengPlusException.cast("教师不存在");
        }
        BeanUtils.copyProperties(courseTeacher,courseTeacher1);
        int i = courseTeacherMapper.updateById(courseTeacher1);
        if(i <= 0) {
            XueChengPlusException.cast("修改教师失败");
        }
        return courseTeacher1;
    }

    @Override
    public void deleteTeacher(Long courseId, Long id) {
        LambdaQueryWrapper<CourseTeacher> qe = new LambdaQueryWrapper<>();
        qe.eq(CourseTeacher::getId, id);
        qe.eq(CourseTeacher::getCourseId, courseId);
        int delete = courseTeacherMapper.delete(qe);
        if(delete <= 0) {
            XueChengPlusException.cast("删除教师失败");
        }
    }
}
